﻿using CSRedis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AIStudio.ConSole.Redis.Ch03
{
    /********************************************************************************************************
    # <start id="pubsub-calls-1"/>
    >>> def publisher(n):
    ...     time.sleep(1)                                                   #A
    ...     for i in xrange(n):
    ...         conn.publish('channel', i)                                  #B
    ...         time.sleep(1)                                               #B
    ...

    >>> def run_pubsub():
    ...     threading.Thread(target = publisher, args = (3,)).start()           #D
    ...     pubsub = conn.pubsub()                                          #E
    ...     pubsub.subscribe(['channel'])                                   #E
    ...     count = 0...     for item in pubsub.listen():                                    #F
    ...         print item                                                  #G
    ...         count += 1                                                  #H
    ...         if count == 4:                                              #H
    ...             pubsub.unsubscribe()                                    #H
    ...         if count == 5:                                              #L
    ...             break                                                   #L
    ...
    >>> run_pubsub()                                                        #C
    { 'pattern': None, 'type': 'subscribe', 'channel': 'channel', 'data': 1L}#I
    { 'pattern': None, 'type': 'message', 'channel': 'channel', 'data': '0'} #J
    { 'pattern': None, 'type': 'message', 'channel': 'channel', 'data': '1'} #J
    { 'pattern': None, 'type': 'message', 'channel': 'channel', 'data': '2'} #J
    {'pattern': None, 'type': 'unsubscribe', 'channel': 'channel', 'data':  #K
    0L}                                                                     #K
    # <end id="pubsub-calls-1"/>
    *********************************************************************************************************/
    public class TestPublisher
    {
        CSRedisClient _conn;
        public TestPublisher()
        {
            _conn = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=14,poolsize=500,ssl=false,writeBuffer=10240");
        }

        public void Publisher(object n)
        {
            Thread.Sleep(1000);
            for (int i = 0; i <= (int)n; i++)
            {
                _conn.Publish("channel", i.ToString());
                Thread.Sleep(1000);
            }
        }

        public void Run_Pubsub()
        {
            Thread thread = new Thread(new ParameterizedThreadStart(Publisher));
            thread.Start(3);

            int count = 0;
            CSRedisClient.SubscribeObject pubsub = null;
            pubsub = _conn.Subscribe(("channel", item =>
            {
                Console.WriteLine(item.Body);
                count += 1;
                if (count == 4)
                {
                    pubsub.Unsubscribe();
                }
            }));
        }
    }
}
